{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对交易策略中一组标记符号进行排名，并设置头寸大小。有了这些功能，你可以轻松优化你的策略，并更有效地管理风险。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<diskcache.core.Cache at 0x234b4178a70>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pybroker\n",
    "from pybroker import Strategy, StrategyConfig, YFinance\n",
    "from pybroker.ext.data import AKShare\n",
    "pybroker.enable_data_source_cache('ranking_and_pos_sizing')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# buy_highest_volume 函数按照最近交易量对股票代码进行排名，并将 100% 的投资组合分配到 2 个柱状图。\n",
    "def buy_highest_volume(ctx):\n",
    "    # If there are no long positions across all tickers being traded:\n",
    "    # 检查 ctx 对象中的 long_positions 方法是否返回任何长期持仓。\n",
    "    # 如果没有（即返回一个空的元组或其他可迭代的空对象），则执行以下的代码块。\n",
    "    if not tuple(ctx.long_positions()):\n",
    "        ctx.buy_shares = ctx.calc_target_shares(1)  #如果没有长期持仓，那么函数会调用 ctx 对象的 calc_target_shares 方法来计算要购买的目标股数。\n",
    "        ctx.hold_bars = 2   #表示要持有购买的这些股票的天数或交易周期。\n",
    "        ctx.score = ctx.volume[-1]  #将 ctx 对象的 score 属性设置为 ctx.volume 的最后一个元素。\n",
    "        #这似乎是基于最新一期的交易量来设置某种评分或优先级。如果 ctx.volume 是一个时间序列数据，\n",
    "        # 那么 -1 索引将获取最新的交易量数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = StrategyConfig(max_long_positions=1)   #持有的多头仓位数量限制为 1\n",
    "strategy = Strategy(AKShare(), '6/1/2021', '6/1/2022', config)\n",
    "strategy.add_execution(buy_highest_volume, ['000005', '000009', '000002', '000001'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = strategy.backtest()\n",
    "result.trades"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "每个股票购买 100 股"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "在 PyBroker 中，你可以根据多个股票代码设置仓位大小。\n",
    "为了说明这一点，我们来看一个简单的买入并持有策略，\n",
    "该策略在 100 天后开始交易，持有仓位 30 天：\n",
    "'''\n",
    "def buy_and_hold(ctx):\n",
    "    if not ctx.long_pos() and ctx.bars >= 100:\n",
    "        ctx.buy_shares = 100\n",
    "        ctx.hold_bars = 30\n",
    "\n",
    "strategy = Strategy(AKShare(), '6/1/2021', '6/1/2022')\n",
    "'''这将在每个股票购买 100 股。'''\n",
    "strategy.add_execution(buy_and_hold, ['000005', '000009', '000002', '000001'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但如果您不想使用等额头寸规模呢？例如，您可能希望将头寸规模设置得更大，以分配更多的股票给波动性较低的股票代码，从而降低投资组合的整体波动性。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个pos_size_handler函数，该函数计算每个股票的头寸大小。\n",
    "# 在交易系统中处理买入位置的大小（即，购买多少份额的股票或其他资产）。\n",
    "import numpy as np\n",
    "# 这个pos_size_handler函数是一个根据过去股价的波动性和当前资金量来动态确定买入份额大小的策略部分。\n",
    "# 这种策略通常用于风险管理，因为它试图在波动率较低的资产上分配更多的资金，以平衡整体风险。\n",
    "def pos_size_handler(ctx):\n",
    "    # Fetch all buy signals.\n",
    "    signals = tuple(ctx.signals(\"buy\")) #从上下文中获取所有“买入”信号，并将它们存储在signals元组中。\n",
    "    # Return if there are no buy signals (i.e. there are only sell signals).\n",
    "    if not signals: #如果没有买入信号（只有卖出信号或其他类型的信号），则函数直接返回，不执行后续操作。\n",
    "        return\n",
    "    # Calculates the inverse volatility, where volatility is defined as the\n",
    "    # standard deviation of close prices for the last 100 days.\n",
    "    '''\n",
    "    这是一个lambda函数，用于计算给定信号的逆波动率。\n",
    "    波动率是通过计算过去100个交易日的收盘价的标准差来确定的。逆波动率是该标准差的倒数。\n",
    "    '''\n",
    "    get_inverse_volatility = lambda signal: 1 / np.std(signal.bar_data.close[-100:])\n",
    "    # Sums the inverse volatilities for all of the buy signals.\n",
    "    '''\n",
    "    使用map函数和上面定义的get_inverse_volatility lambda函数来计算所有买入信号的逆波动率之和。\n",
    "    '''\n",
    "    total_inverse_volatility = sum(map(get_inverse_volatility, signals))\n",
    "    for signal in signals:  #对于每个买入信号，使用其逆波动率占总逆波动率的比例来计算购买份额的大小。\n",
    "        size = get_inverse_volatility(signal) / total_inverse_volatility\n",
    "        # Calculate the number of shares given the latest close price.\n",
    "        # 根据资金量（在这里是95,000元）和最后一个交易日的收盘价来计算可以购买的份额数量。\n",
    "        shares = ctx.calc_target_shares(size, signal.bar_data.close[-1], cash=95_000)\n",
    "        ctx.set_shares(signal, shares)#使用set_shares方法（也是在ctx中定义的）来设置每个买入信号的购买份额。\n",
    "\n",
    "strategy.set_pos_size_handler(pos_size_handler) #当该策略需要确定买入位置的大小时，它将调用这个pos_size_handler函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Backtesting: 2021-06-01 00:00:00 to 2022-06-01 00:00:00\n",
      "\n",
      "Loaded cached bar data.\n",
      "\n",
      "Test split: 2021-06-01 00:00:00 to 2022-06-01 00:00:00\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0% (0 of 243) |                        | Elapsed Time: 0:00:00 ETA:  --:--:--\n",
      " 41% (101 of 243) |#########             | Elapsed Time: 0:00:00 ETA:   0:00:00\n",
      " 45% (111 of 243) |##########            | Elapsed Time: 0:00:00 ETA:   0:00:00\n",
      " 49% (121 of 243) |##########            | Elapsed Time: 0:00:00 ETA:   0:00:00\n",
      " 53% (131 of 243) |###########           | Elapsed Time: 0:00:00 ETA:   0:00:00\n",
      " 58% (141 of 243) |############          | Elapsed Time: 0:00:01 ETA:   0:00:00\n",
      " 62% (151 of 243) |#############         | Elapsed Time: 0:00:01 ETA:   0:00:00\n",
      " 66% (161 of 243) |##############        | Elapsed Time: 0:00:01 ETA:   0:00:00\n",
      " 70% (171 of 243) |###############       | Elapsed Time: 0:00:01 ETA:   0:00:00\n",
      " 74% (181 of 243) |################      | Elapsed Time: 0:00:01 ETA:   0:00:00\n",
      " 78% (191 of 243) |#################     | Elapsed Time: 0:00:01 ETA:   0:00:00\n",
      " 82% (201 of 243) |##################    | Elapsed Time: 0:00:02 ETA:   0:00:00\n",
      " 86% (211 of 243) |###################   | Elapsed Time: 0:00:02 ETA:   0:00:00\n",
      " 90% (221 of 243) |####################  | Elapsed Time: 0:00:02 ETA:   0:00:00\n",
      " 95% (231 of 243) |####################  | Elapsed Time: 0:00:02 ETA:   0:00:00\n",
      " 99% (241 of 243) |##################### | Elapsed Time: 0:00:02 ETA:   0:00:00\n",
      "100% (243 of 243) |######################| Elapsed Time: 0:00:02 Time:  0:00:02\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Finished backtest: 0:00:03\n"
     ]
    }
   ],
   "source": [
    "result = strategy.backtest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>symbol</th>\n",
       "      <th>entry_date</th>\n",
       "      <th>exit_date</th>\n",
       "      <th>entry</th>\n",
       "      <th>exit</th>\n",
       "      <th>shares</th>\n",
       "      <th>pnl</th>\n",
       "      <th>return_pct</th>\n",
       "      <th>agg_pnl</th>\n",
       "      <th>bars</th>\n",
       "      <th>pnl_per_bar</th>\n",
       "      <th>stop</th>\n",
       "      <th>mae</th>\n",
       "      <th>mfe</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>long</td>\n",
       "      <td>000005</td>\n",
       "      <td>2021-10-29</td>\n",
       "      <td>2021-12-10</td>\n",
       "      <td>2.16</td>\n",
       "      <td>2.24</td>\n",
       "      <td>36242</td>\n",
       "      <td>2899.36</td>\n",
       "      <td>3.70</td>\n",
       "      <td>2899.36</td>\n",
       "      <td>30</td>\n",
       "      <td>96.65</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.12</td>\n",
       "      <td>0.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>long</td>\n",
       "      <td>000009</td>\n",
       "      <td>2021-10-29</td>\n",
       "      <td>2021-12-10</td>\n",
       "      <td>20.73</td>\n",
       "      <td>16.81</td>\n",
       "      <td>141</td>\n",
       "      <td>-552.72</td>\n",
       "      <td>-18.91</td>\n",
       "      <td>2346.64</td>\n",
       "      <td>30</td>\n",
       "      <td>-18.42</td>\n",
       "      <td>bar</td>\n",
       "      <td>-4.71</td>\n",
       "      <td>0.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>long</td>\n",
       "      <td>000002</td>\n",
       "      <td>2021-10-29</td>\n",
       "      <td>2021-12-10</td>\n",
       "      <td>18.59</td>\n",
       "      <td>20.23</td>\n",
       "      <td>347</td>\n",
       "      <td>569.08</td>\n",
       "      <td>8.82</td>\n",
       "      <td>2915.72</td>\n",
       "      <td>30</td>\n",
       "      <td>18.97</td>\n",
       "      <td>bar</td>\n",
       "      <td>-1.07</td>\n",
       "      <td>2.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2021-10-29</td>\n",
       "      <td>2021-12-10</td>\n",
       "      <td>19.53</td>\n",
       "      <td>18.48</td>\n",
       "      <td>324</td>\n",
       "      <td>-340.20</td>\n",
       "      <td>-5.38</td>\n",
       "      <td>2575.52</td>\n",
       "      <td>30</td>\n",
       "      <td>-11.34</td>\n",
       "      <td>bar</td>\n",
       "      <td>-2.37</td>\n",
       "      <td>0.21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>long</td>\n",
       "      <td>000005</td>\n",
       "      <td>2021-12-13</td>\n",
       "      <td>2022-01-25</td>\n",
       "      <td>2.22</td>\n",
       "      <td>2.09</td>\n",
       "      <td>36551</td>\n",
       "      <td>-4751.63</td>\n",
       "      <td>-5.86</td>\n",
       "      <td>-2176.11</td>\n",
       "      <td>30</td>\n",
       "      <td>-158.39</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.14</td>\n",
       "      <td>0.24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>long</td>\n",
       "      <td>000009</td>\n",
       "      <td>2021-12-13</td>\n",
       "      <td>2022-01-25</td>\n",
       "      <td>16.95</td>\n",
       "      <td>14.01</td>\n",
       "      <td>130</td>\n",
       "      <td>-382.20</td>\n",
       "      <td>-17.35</td>\n",
       "      <td>-2558.31</td>\n",
       "      <td>30</td>\n",
       "      <td>-12.74</td>\n",
       "      <td>bar</td>\n",
       "      <td>-3.50</td>\n",
       "      <td>0.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>long</td>\n",
       "      <td>000002</td>\n",
       "      <td>2021-12-13</td>\n",
       "      <td>2022-01-25</td>\n",
       "      <td>20.20</td>\n",
       "      <td>21.20</td>\n",
       "      <td>247</td>\n",
       "      <td>247.00</td>\n",
       "      <td>4.95</td>\n",
       "      <td>-2311.31</td>\n",
       "      <td>30</td>\n",
       "      <td>8.23</td>\n",
       "      <td>bar</td>\n",
       "      <td>-1.17</td>\n",
       "      <td>2.89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2021-12-13</td>\n",
       "      <td>2022-01-25</td>\n",
       "      <td>18.53</td>\n",
       "      <td>16.94</td>\n",
       "      <td>407</td>\n",
       "      <td>-647.13</td>\n",
       "      <td>-8.58</td>\n",
       "      <td>-2958.44</td>\n",
       "      <td>30</td>\n",
       "      <td>-21.57</td>\n",
       "      <td>bar</td>\n",
       "      <td>-2.41</td>\n",
       "      <td>0.35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>long</td>\n",
       "      <td>000005</td>\n",
       "      <td>2022-01-26</td>\n",
       "      <td>2022-03-16</td>\n",
       "      <td>2.06</td>\n",
       "      <td>2.07</td>\n",
       "      <td>38856</td>\n",
       "      <td>388.56</td>\n",
       "      <td>0.49</td>\n",
       "      <td>-2569.88</td>\n",
       "      <td>30</td>\n",
       "      <td>12.95</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.03</td>\n",
       "      <td>0.27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>long</td>\n",
       "      <td>000009</td>\n",
       "      <td>2022-01-26</td>\n",
       "      <td>2022-03-16</td>\n",
       "      <td>13.46</td>\n",
       "      <td>11.02</td>\n",
       "      <td>160</td>\n",
       "      <td>-390.40</td>\n",
       "      <td>-18.13</td>\n",
       "      <td>-2960.28</td>\n",
       "      <td>30</td>\n",
       "      <td>-13.01</td>\n",
       "      <td>bar</td>\n",
       "      <td>-2.71</td>\n",
       "      <td>1.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>long</td>\n",
       "      <td>000002</td>\n",
       "      <td>2022-01-26</td>\n",
       "      <td>2022-03-16</td>\n",
       "      <td>20.80</td>\n",
       "      <td>15.12</td>\n",
       "      <td>291</td>\n",
       "      <td>-1652.88</td>\n",
       "      <td>-27.31</td>\n",
       "      <td>-4613.16</td>\n",
       "      <td>30</td>\n",
       "      <td>-55.10</td>\n",
       "      <td>bar</td>\n",
       "      <td>-6.03</td>\n",
       "      <td>0.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2022-01-26</td>\n",
       "      <td>2022-03-16</td>\n",
       "      <td>16.82</td>\n",
       "      <td>13.96</td>\n",
       "      <td>406</td>\n",
       "      <td>-1161.16</td>\n",
       "      <td>-17.00</td>\n",
       "      <td>-5774.32</td>\n",
       "      <td>30</td>\n",
       "      <td>-38.71</td>\n",
       "      <td>bar</td>\n",
       "      <td>-3.60</td>\n",
       "      <td>0.52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>long</td>\n",
       "      <td>000005</td>\n",
       "      <td>2022-03-17</td>\n",
       "      <td>2022-05-05</td>\n",
       "      <td>2.12</td>\n",
       "      <td>1.81</td>\n",
       "      <td>38941</td>\n",
       "      <td>-12071.71</td>\n",
       "      <td>-14.62</td>\n",
       "      <td>-17846.03</td>\n",
       "      <td>30</td>\n",
       "      <td>-402.39</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.31</td>\n",
       "      <td>0.24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>long</td>\n",
       "      <td>000009</td>\n",
       "      <td>2022-03-17</td>\n",
       "      <td>2022-05-05</td>\n",
       "      <td>11.94</td>\n",
       "      <td>9.15</td>\n",
       "      <td>214</td>\n",
       "      <td>-597.06</td>\n",
       "      <td>-23.37</td>\n",
       "      <td>-18443.09</td>\n",
       "      <td>30</td>\n",
       "      <td>-19.90</td>\n",
       "      <td>bar</td>\n",
       "      <td>-4.02</td>\n",
       "      <td>0.43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>long</td>\n",
       "      <td>000002</td>\n",
       "      <td>2022-03-17</td>\n",
       "      <td>2022-05-05</td>\n",
       "      <td>16.58</td>\n",
       "      <td>19.29</td>\n",
       "      <td>275</td>\n",
       "      <td>745.25</td>\n",
       "      <td>16.34</td>\n",
       "      <td>-17697.84</td>\n",
       "      <td>30</td>\n",
       "      <td>24.84</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.30</td>\n",
       "      <td>5.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2022-03-17</td>\n",
       "      <td>2022-05-05</td>\n",
       "      <td>14.48</td>\n",
       "      <td>15.30</td>\n",
       "      <td>314</td>\n",
       "      <td>257.48</td>\n",
       "      <td>5.66</td>\n",
       "      <td>-17440.36</td>\n",
       "      <td>30</td>\n",
       "      <td>8.58</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.34</td>\n",
       "      <td>2.18</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    type  symbol entry_date  exit_date  entry   exit  shares       pnl  \\\n",
       "id                                                                       \n",
       "1   long  000005 2021-10-29 2021-12-10   2.16   2.24   36242   2899.36   \n",
       "2   long  000009 2021-10-29 2021-12-10  20.73  16.81     141   -552.72   \n",
       "3   long  000002 2021-10-29 2021-12-10  18.59  20.23     347    569.08   \n",
       "4   long  000001 2021-10-29 2021-12-10  19.53  18.48     324   -340.20   \n",
       "5   long  000005 2021-12-13 2022-01-25   2.22   2.09   36551  -4751.63   \n",
       "6   long  000009 2021-12-13 2022-01-25  16.95  14.01     130   -382.20   \n",
       "7   long  000002 2021-12-13 2022-01-25  20.20  21.20     247    247.00   \n",
       "8   long  000001 2021-12-13 2022-01-25  18.53  16.94     407   -647.13   \n",
       "9   long  000005 2022-01-26 2022-03-16   2.06   2.07   38856    388.56   \n",
       "10  long  000009 2022-01-26 2022-03-16  13.46  11.02     160   -390.40   \n",
       "11  long  000002 2022-01-26 2022-03-16  20.80  15.12     291  -1652.88   \n",
       "12  long  000001 2022-01-26 2022-03-16  16.82  13.96     406  -1161.16   \n",
       "13  long  000005 2022-03-17 2022-05-05   2.12   1.81   38941 -12071.71   \n",
       "14  long  000009 2022-03-17 2022-05-05  11.94   9.15     214   -597.06   \n",
       "15  long  000002 2022-03-17 2022-05-05  16.58  19.29     275    745.25   \n",
       "16  long  000001 2022-03-17 2022-05-05  14.48  15.30     314    257.48   \n",
       "\n",
       "    return_pct   agg_pnl  bars  pnl_per_bar stop   mae   mfe  \n",
       "id                                                            \n",
       "1         3.70   2899.36    30        96.65  bar -0.12  0.22  \n",
       "2       -18.91   2346.64    30       -18.42  bar -4.71  0.85  \n",
       "3         8.82   2915.72    30        18.97  bar -1.07  2.25  \n",
       "4        -5.38   2575.52    30       -11.34  bar -2.37  0.21  \n",
       "5        -5.86  -2176.11    30      -158.39  bar -0.14  0.24  \n",
       "6       -17.35  -2558.31    30       -12.74  bar -3.50  0.25  \n",
       "7         4.95  -2311.31    30         8.23  bar -1.17  2.89  \n",
       "8        -8.58  -2958.44    30       -21.57  bar -2.41  0.35  \n",
       "9         0.49  -2569.88    30        12.95  bar -0.03  0.27  \n",
       "10      -18.13  -2960.28    30       -13.01  bar -2.71  1.09  \n",
       "11      -27.31  -4613.16    30       -55.10  bar -6.03  0.88  \n",
       "12      -17.00  -5774.32    30       -38.71  bar -3.60  0.52  \n",
       "13      -14.62 -17846.03    30      -402.39  bar -0.31  0.24  \n",
       "14      -23.37 -18443.09    30       -19.90  bar -4.02  0.43  \n",
       "15       16.34 -17697.84    30        24.84  bar -0.30  5.32  \n",
       "16        5.66 -17440.36    30         8.58  bar -0.34  2.18  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.trades"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pybroker",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
